package com.spillman.signtab.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.spillman.signtab.R;
import com.spillman.signtab.dummy.DummyContent;
import com.spillman.signtab.ui.ItemDetailActivity;
import com.spillman.signtab.ui.ItemListActivity;
import com.spillman.signtab.utils.FragmentCallbacks;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
 * {@link ItemDetailActivity} on handsets.
 */
public class ItemDetailFragment extends ListFragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";

	/**
	 * The dummy content this fragment is presenting.
	 */
	private DummyContent.DummyItem mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public ItemDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
		}
	}

	private View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			((FragmentCallbacks) getActivity()).onFragmentResponse(null);
		}
	};

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		String headerText = "";
		TextView header = (TextView) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
				.inflate(android.R.layout.simple_list_item_1, null, false);
		View footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.button, null, false);
		footer.setOnClickListener(listener);
		if (mItem != null) {
			headerText = mItem.content + " Options";
		}
		header.setText(headerText);
		header.setBackgroundColor(getResources().getColor(R.color.grey_d4d4d4));
		header.setGravity(Gravity.CENTER);
		getListView().addHeaderView(header);
		setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item_with_checkbox, R.id.optionName,
				new String[] { "Subject Signature", "Take Photos", "Audio Record" }));
		getListView().addFooterView(footer);
		super.onViewCreated(view, savedInstanceState);
	}
}
